#include "Vector3.h"

Vector3::Vector3(float _x, float _y, float _z)
{
	x = _x;
	y = _y;
	z = _z;
};

Vector3::~Vector3()
{
};

void Vector3::Set(float x, float y, float z)
{
	Vector3(x, y, z);
};

void Vector3::Normalize()
{
	float length = Length();
	x /= length;
	y /= length;
	z /= length;
};

float Vector3::Length()
{
	return sqrt(x * x + y * y + z * z);
};

Vector3 Vector3::operator+(const Vector3 &v)
{
	return Vector3(x + v.x, y + v.y, z + v.z);
};

Vector3 Vector3::operator-()
{
	return Vector3(-x, -y, -z);
};

Vector3 Vector3::operator*(const float f)
{
	return Vector3(x * f, y * f, z * f);
};

Vector3 Vector3::operator*(const Vector3 &v)
{
	return Vector3(y * v.z - v.y * z, z * v.x - x * v.z, x * v.y - y * v.z);
};

Vector3 &Vector3::operator*=(const float f)
{
	x *= f;
	y *= f;
	z *= f;
	return *this;
};

Vector3 &Vector3::operator*=(const Vector3 &v)
{
	*this = *this * v;
	return *this;
};

Vector3 &Vector3::operator+=(const Vector3 &v)
{
	x += v.x;
	y += v.y;
	z += v.z;
	return *this;
};

Vector3 &Vector3::operator-=(const Vector3 &v)
{
	x -= v.x;
	y -= v.y;
	z -= v.z;
	return *this;
};

Vector3 operator*(const Matrix &matrix, const Vector3 &vector)
{
	return Vector3(matrix.value[0] * vector.x + matrix.value[4] * vector.y + matrix.value[8] * vector.z + matrix.value[12],
		matrix.value[1] * vector.x + matrix.value[5] * vector.y + matrix.value[9] * vector.z + matrix.value[13],
		matrix.value[2] * vector.x + matrix.value[6] * vector.y + matrix.value[10] * vector.z + matrix.value[14]);
};

Vector3 operator-(const Vector3 &vector1, const Vector3 &vector2)
{
	return Vector3(vector1.x - vector2.x, vector1.y - vector2.y, vector1.z - vector2.z); 
};

Vector3 Vector3::projection(const Vector3 &v)
{
	Vector3 tmp = v;
	float cosAngle = (x*v.x + y*v.y + z*v.z)/(Length() * tmp.Length());
	tmp.Normalize();
	return (tmp*Length()*cosAngle);
};
